package site.zhouinfo.utils;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Random;
import javax.imageio.ImageIO;

import static java.awt.image.BufferedImage.TYPE_INT_RGB;

/**
 * 验证码工具类
 *
 * @author zhou
 * @create 2017-06-27 11:00
 * @email zhouinfo@qq.com
 * @blog blog.zhouinfo.site
 */
public class IdentifyCodeUtil {

    public static void writeOutputStream(OutputStream out) throws IOException {
        ImageIO.write(getIdentifyCodeImage(), "jpeg", out);
        out.flush();
        out.close();
    }

    public static BufferedImage getIdentifyCodeImage() {
        BufferedImage image = new BufferedImage(80, 20, TYPE_INT_RGB);
        Graphics g = image.getGraphics();
        g.setColor(Color.lightGray);
        g.fillRect(0, 0, 80, 20);
        Random random = new Random();
        for (int i = 0; i < 4; i++) {
            String s = random.nextInt(10) + "";
            g.setColor(new Color(random.nextInt(100), random.nextInt(100), random.nextInt(100)));
            g.drawString(s, 20 * i + 6, 16);
        }
        g.dispose();
        image.flush();
        return image;
    }

}
